<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      width: 300px;
      height: 40px;
      border: 1px solid pink;
      text-align: center;
      line-height: 40px;
    }
  </style>
</head>

<body>
  <div></div>

  <script>
    const divs = document.querySelector('div')

    // 实现电子时间效果，在div里显示 现在是2024年11月21日 10:12:07 格式的时间
    function getTime() {
      const date = new Date()
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      let h = date.getHours()
      h = h < 10 ? '0' + h : h

      let m = date.getMinutes()
      m = m < 10 ? '0' + m : m

      let s = date.getSeconds()
      s = s < 10 ? '0' + s : s
      
      divs.innerHTML = `现在是:${year}年${month}月${day}日${h}:${m}:${s}`
    }

    getTime()
    setInterval(getTime, 1000)
  </script>
</body>

</html>